home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 July / macformat52.iso / mac / Shareware Plus / Developers / YAAF v1.0 alpha 1 / Headers / Core / XAutoPtr.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-24  |  3.2 KB  |  116 lines

  1. /*    XAutoPtr.h
  2.  *
  3.  *        Support templates that I can use that are not yet defined
  4.  *    for me.
  5.  */
  6.  
  7. /*  YAAF - Yet another application framework
  8.  *  Copyright (C) 1997 William Edward Woody and In Phase Consulting
  9.  *  
  10.  *  This library is free software; you can redistribute it
  11.  *  and/or modify it under the terms of the GNU Library
  12.  *  General Public License as published by the Free Software
  13.  *  Foundation; either version 2 of the License, or any
  14.  *  later version.
  15.  *  
  16.  *  This library is distributed in the hope that it will be
  17.  *  useful, but WITHOUT ANY WARRANTY; without even the implied
  18.  *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
  19.  *  PURPOSE. See the GNU Library General Public License for
  20.  *  more details.
  21.  *  
  22.  *  You should have received a copy of the GNU Library General
  23.  *  Public License along with this library; if not, write to the
  24.  *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  25.  *  Boston, MA 02111-1307, USA.
  26.  *  
  27.  *  To contact the author, either e-mail me at
  28.  *  woody@alumni.caltech.edu, or write to us at
  29.  *  
  30.  *          William Edward Woody
  31.  *          In Phase Consulting
  32.  *          1545 Ard Eevin Avenue
  33.  *          Glendale, CA 91202
  34.  */
  35.  
  36. #ifndef __XAUTOPTR_H__
  37. #define __XAUTOPTR_H__
  38.  
  39. #if defined(__MWERKS__)
  40.     #if defined(macintosh)
  41.         #pragma options align=power
  42.     #endif
  43.     #if defined(__INTEL__)
  44.         #pragma pack(push,2)
  45.     #endif
  46. #endif
  47.  
  48. /************************************************************************/
  49. /*                                                                        */
  50. /*    Heap Management Stuff                                                */
  51. /*                                                                        */
  52. /************************************************************************/
  53.  
  54. /*    autoptr
  55.  *
  56.  *        Automatic pointer, like auto_ptr, except that I did it myself.
  57.  *
  58.  *    The rules are simple: this is a substitute for T*ptr, except that
  59.  *    I cannot manipulate the pointer, I cannot copy to another pointer,
  60.  *    and I must either explicitly release the pointer to the world
  61.  *    (like TWindow objects) or return the value--both cause this pointer
  62.  *    to become NULL.
  63.  */
  64.  
  65. template <class T> class autoptr {
  66.     public:
  67.         autoptr(T *ptr = NULL)        { v = ptr; }
  68.         ~autoptr()                    { if (v) delete v; }
  69.         const autoptr& operator = (T* ptr) { v = ptr; return *this; }
  70.         
  71.         /*
  72.          *    Access to pointer
  73.          */
  74.         
  75.         /*
  76.          *        If the compiler barfs on the next line, it's because
  77.          *    you declared an autoptr<> with a fundamental type, such
  78.          *    as an int, a double, or a float.
  79.          */
  80.         
  81.         T* operator ->()            { return v; }    // ERROR ^^^
  82.         
  83.         
  84.         T& operator *()                { return *v; }
  85. //        T& operator [](long i)        { return *(v+i); }
  86.         int operator !()            { return (v == NULL); }
  87.         int operator == (const T* a) { return (v == a); }
  88.         int operator != (const T* a) { return (v != a); }
  89.         int operator == (const autoptr& ptr) { return (v == ptr.v); }
  90.         int operator != (const autoptr& ptr) { return (v != ptr.v); }
  91.         
  92.         /*
  93.          *    Random things I can do to this pointer
  94.          */
  95.         
  96.         void Detach()                { v = NULL; }
  97.         T*   Return()                { T *a; a = v; v = NULL; return a; }
  98.         T*   Ptr()                    { return v; }
  99.         
  100.     private:
  101.         const autoptr& operator = (const autoptr& ptr) { return ptr; } // prohibit
  102.         
  103.         T *v;
  104. };
  105.  
  106. #if defined(__MWERKS__)
  107.     #if defined(macintosh)
  108.         #pragma options align=reset
  109.     #endif
  110.     #if defined(__INTEL__)
  111.         #pragma pack(pop)
  112.     #endif
  113. #endif
  114.  
  115. #endif // __XAUTOPTR_H__
  116.